{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Symbol 是解方程的库\n",
    "import pandas as pd\n",
    "from sympy import Symbol, Eq, solve, summation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [],
   "source": [
    "eq_list0 = []\n",
    "steady_state_rate = Symbol('steady_state_rate')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 公式一"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$N_k =\\frac{y_k}{\\mu_{N}}$ ，$y_k$是资本品部门的真实产出，$\\mu_{N}$是资本品部门的劳动生产率，$N_k$是为了满足产出所需要的劳动力"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [],
   "source": [
    "number_of_capital_firm_workers = Symbol('number_of_capital_firm_workers')\n",
    "output_of_capital = Symbol('output_of_capital')\n",
    "labor_productivity = Symbol('labor_productivity')\n",
    "eq_list0.append(Eq(number_of_capital_firm_workers, output_of_capital / labor_productivity))\n",
    "# 4个未知数，1个方程"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 公式二\n",
    "$uc_k = \\frac{WN_k}{y_k} = \\frac{W}{\\mu_N}$ ，$uc_k$为k产品的成本，$W$为工人工资，$\\mu_N$为劳动生产率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [],
   "source": [
    "unit_cost_of_capital_firm = Symbol('unit_cost_of_capital_firm')\n",
    "households_wage = Symbol('households_wage')\n",
    "eq_list0.append(Eq(unit_cost_of_capital_firm, households_wage / labor_productivity))\n",
    "# 6个未知数 2个方程"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 公式3\n",
    "$p_k = (1+mu_k)uc_k$   <br>$p_k$为产品价格，$mu_k$为k产品加价比例，$uc_k$为k产品的成本"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [],
   "source": [
    "# eq3\n",
    "price_of_capital = Symbol('price_of_capital')\n",
    "mark_up_of_capital = Symbol('mark_up_of_capital')\n",
    "eq_list0.append(Eq(price_of_capital, (1 + mark_up_of_capital) * unit_cost_of_capital_firm))\n",
    "# 8个未知数 3个方程"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 公式4\n",
    "$D_k = \\sigma{WN_k}$  <br>公司愿意持有的存款$D_k$(<font color=\"red\">deposits_capital_firms_holds</font>)等比例于需要发放工资的数量，$\\sigma$（<font color=\"red\">ratio_of_deposits_to_wage</font>）等于比例系数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [],
   "source": [
    "# eq4\n",
    "deposits_capital_firms_holds = Symbol('deposits_capital_firms_holds')\n",
    "ratio_of_deposits_to_wage = Symbol('ratio_of_deposits_to_wage')\n",
    "eq_list0.append(\n",
    "    Eq(deposits_capital_firms_holds, ratio_of_deposits_to_wage * households_wage * number_of_capital_firm_workers)\n",
    ")\n",
    "# 10个未知数 4 个方程"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 公式5\n",
    "$y_k = (i_c) = \\frac{K}{\\kappa}$ <br>\n",
    "公司的产出$y_k$等于消费品公司的投资$i_c$,也等于消费品持有的资本总量$K$处于折旧年限$\\kappa$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [],
   "source": [
    "# eq5\n",
    "capital_of_consumption_firm = Symbol('capital_of_consumption_firm')\n",
    "duration_or_capital = Symbol('duration_or_capital')\n",
    "eq_list0.append(Eq(output_of_capital, capital_of_consumption_firm / duration_or_capital))\n",
    "# 12个未知数 5个方程"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 公式6\n",
    "$inv_k = \\nu{y_k}$ <br>\n",
    "K公司愿意持有存货的量$inv_k$(<font color=\"red\">capital_firm_inventory</font>)等比例于$\\nu$(<font color=\"red\">ratio_of_inventory_to_output</font>)销售量$y_k$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [],
   "source": [
    "# eq6\n",
    "capital_firm_inventory = Symbol('capital_firm_inventory')\n",
    "ratio_of_inventory_to_output = Symbol('ratio_of_inventory_to_output')\n",
    "eq_list0.append(Eq(capital_firm_inventory, ratio_of_inventory_to_output * output_of_capital))\n",
    "# 14个未知数 6个方程"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 公式7-8"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$\\pi_k = {p_ky_k} + \\frac{i^d{D_k}}{1+g_{ss}} +{inv_k}\\frac{g_{ss}}{1+g_{ss}}{uc_{k}}-WN_k - \\frac{i^lL_k}{1+g_{ss}}$ <br>\n",
    "$\\pi_k$ 企业利润 <br>\n",
    "$p_ky_k$ 价格×销量 <br>\n",
    "$\\frac{i^d{D_k}}{1+g_{ss}}$ 存款利息收入 <br>\n",
    "${T_k} = {\\pi_k} \\tau_k$ <br>\n",
    "公司交税等于利润乘以税率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [],
   "source": [
    "# eq7\n",
    "profits_of_capital_firm_before_tax = Symbol('profits_of_capital_firm_before_tax')\n",
    "profits_of_capital_firm_after_tax = Symbol('profits_of_capital_firm_after_tax')\n",
    "\n",
    "interest_rate_of_deposits = Symbol('interest_rate_of_deposits')\n",
    "interest_rate_of_loans = Symbol('interest_rate_of_loans')\n",
    "loans_of_capital_firms = Symbol('loans_of_capital_firms')\n",
    "deposits_interests_of_capital_firm = Symbol('deposits_interests_of_capital_firm')\n",
    "loan_interests_of_capital_firm = Symbol('loan_interests_of_capital_firm')\n",
    "change_inventory_of_capital_firm = Symbol('change_inventory_of_capital_firm')\n",
    "\n",
    "oo1_steady_state_rate = Symbol('oo1_steady_state_rate')\n",
    "oo2_steady_state_rate = Symbol('oo2_steady_state_rate')\n",
    "tax_of_capital_firms = Symbol('tax_of_capital_firms')\n",
    "tax_rate = Symbol('tax_rate')\n",
    "\n",
    "eq_list0.append(Eq(oo1_steady_state_rate, 1 / (steady_state_rate + 1)))\n",
    "eq_list0.append(Eq(oo2_steady_state_rate, steady_state_rate / (steady_state_rate + 1)))\n",
    "eq_list0.append(\n",
    "    Eq(deposits_interests_of_capital_firm, interest_rate_of_deposits * deposits_capital_firms_holds * oo1_steady_state_rate)\n",
    ")\n",
    "eq_list0.append(Eq(loan_interests_of_capital_firm, loans_of_capital_firms * interest_rate_of_loans * oo1_steady_state_rate))\n",
    "eq_list0.append(Eq(change_inventory_of_capital_firm, capital_firm_inventory * unit_cost_of_capital_firm * oo2_steady_state_rate))\n",
    "eq_list0.append(Eq(profits_of_capital_firm_before_tax,\n",
    "                  price_of_capital * output_of_capital +\n",
    "                  deposits_interests_of_capital_firm +\n",
    "                  change_inventory_of_capital_firm -\n",
    "                  households_wage * number_of_capital_firm_workers -\n",
    "                  loan_interests_of_capital_firm\n",
    "                  ))\n",
    "eq_list0.append(Eq(tax_of_capital_firms, tax_rate * profits_of_capital_firm_before_tax))\n",
    "eq_list0.append(Eq(profits_of_capital_firm_after_tax, profits_of_capital_firm_before_tax - tax_of_capital_firms))\n",
    "# 26个未知数，14个方程"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 公式9\n",
    "${Div_k} = \\rho_k[{\\pi_k(1-\\tau_k)}]$ <br>\n",
    "公司股利发放等比例于扣税后的净利润"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [],
   "source": [
    "# eq9\n",
    "dividends_of_capital_firms = Symbol('dividends_of_capital_firms')\n",
    "ratio_of_dividends_to_profits_left = Symbol('ratio_of_dividends_to_profits_left')\n",
    "eq_list0.append(Eq(dividends_of_capital_firms,\n",
    "                  ratio_of_dividends_to_profits_left * (profits_of_capital_firm_before_tax - tax_of_capital_firms)\n",
    "                  ))\n",
    "# 28个未知数，16个方程"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 公式10\n",
    "${L_k}(\\frac{g_{ss}}{1+g_{ss}}) = ({inv_k} \\frac{g_{ss}}{1+g_{ss}}{uc_k}) + {D_k}\\frac{g_{ss}}{1+g_{ss}} - ({\\pi_k}-{T_k} - {Div_k})$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [],
   "source": [
    "# eq10\n",
    "nominal_output_of_capital = Symbol('nominal_output_of_capital')\n",
    "delta_loans_of_capital_firms = Symbol('delta_loans_of_capital_firms')\n",
    "eq_list0.append(Eq(delta_loans_of_capital_firms, loans_of_capital_firms * oo2_steady_state_rate))\n",
    "eq_list0.append(Eq(nominal_output_of_capital, output_of_capital * price_of_capital))\n",
    "eq_list0.append(Eq(delta_loans_of_capital_firms,\n",
    "                  capital_firm_inventory * unit_cost_of_capital_firm * oo2_steady_state_rate +\n",
    "                  deposits_capital_firms_holds * oo2_steady_state_rate -\n",
    "                  (profits_of_capital_firm_before_tax - tax_of_capital_firms - dividends_of_capital_firms)\n",
    "                  ))\n",
    "# 30个未知数，19个方程"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [],
   "source": [
    "variable_collector0 = {\n",
    "    'steady_state_rate': (steady_state_rate, \"稳态增速\", \"外生设定\"),                                     # 1\n",
    "    'number_of_capital_firm_workers': (number_of_capital_firm_workers, \"资本公司员工数量\", \"外生设定\"),    # 2\n",
    "    'output_of_capital': (output_of_capital, \"资本品的产出\",\"-\"),                                         # 3\n",
    "    'labor_productivity': (labor_productivity, \"生产资本品劳动生产率\",\"-\"),                                # 4\n",
    "    'unit_cost_of_capital_firm': (unit_cost_of_capital_firm, \"资本品单位生产成本\",\"-\"),                    # 5\n",
    "    'households_wage': (households_wage, \"员工工资\",\"外生设定\"),                                           # 6\n",
    "    'price_of_capital': (price_of_capital, \"资本品价格\",\"-\"),                                             # 7\n",
    "    'mark_up_of_capital': (mark_up_of_capital, \"资本加价比例\",\"外生设定\"),                                 # 8\n",
    "    'deposits_capital_firms_holds': (deposits_capital_firms_holds, \"资本品公司存款\",\"-\"),                 # 9\n",
    "    'ratio_of_deposits_to_wage': (ratio_of_deposits_to_wage, \"公司持有存款占应付工资比例\",\"外生设定\"),      # 10\n",
    "    'capital_of_consumption_firm': (capital_of_consumption_firm, \"消费品公司所持有的资本\",\"-\"),            # 11\n",
    "    'duration_or_capital': (duration_or_capital, \"资本折旧年限\",\"外生设定\"),                               # 12\n",
    "    'capital_firm_inventory': (capital_firm_inventory, \"资本品公司存货\",\"-\"),                             # 13\n",
    "    'ratio_of_inventory_to_output': (ratio_of_inventory_to_output, \"资本品公司存货比例\",\"外生设定\"),       # 14\n",
    "    'profits_of_capital_firm_before_tax': (profits_of_capital_firm_before_tax, \"资本品公司税前利润\",\"-\"),     # 15\n",
    "    'interest_rate_of_deposits': (interest_rate_of_deposits,\"存款利率\",\"外生设定\"),                       # 16\n",
    "    'interest_rate_of_loans': (interest_rate_of_loans, \"贷款利率\",\"外生设定\"),                            # 17\n",
    "    'loans_of_capital_firms': (loans_of_capital_firms, \"资本品公司贷款\",\"-\"),                             # 18\n",
    "    'deposits_interests_of_capital_firm':(deposits_interests_of_capital_firm, \"资本品公司存款利息\", \"-\"), # 19\n",
    "    'loan_interests_of_capital_firm':(loan_interests_of_capital_firm,\"资本品公司贷款利息\",\"-\"),           # 20\n",
    "    'change_inventory_of_capital_firm':(change_inventory_of_capital_firm, \"资本品公司存货增加\", \"-\"),     # 21\n",
    "    'tax_of_capital_firms': (tax_of_capital_firms, \"资本品公司税收\",\"-\"),\n",
    "    'profits_of_capital_firm_after_tax': (profits_of_capital_firm_after_tax, \"资本品公司税后利润\", \"-\"),  \n",
    "    'tax_rate': (tax_rate, \"税率\",\"外生设定\"),\n",
    "    'dividends_of_capital_firms': (dividends_of_capital_firms, \"资本品公司股利\",\"-\"),\n",
    "    'ratio_of_dividends_to_profits_left': (ratio_of_dividends_to_profits_left, \"公司股利比率\",\"外生设定\"),\n",
    "    'delta_loans_of_capital_firms':(delta_loans_of_capital_firms, \"资本品公司贷款增加\", \"-\"),\n",
    "    'oo1_steady_state_rate': (oo1_steady_state_rate, \"辅助参数1\",\"外生设定\"),\n",
    "    'oo2_steady_state_rate': (oo2_steady_state_rate, \"辅助参数2\",\"外生设定\"),\n",
    "    'nominal_output_of_capital':(nominal_output_of_capital, \"资本品名义产出\", '-'),\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [],
   "source": [
    "eq_list0.append(Eq(steady_state_rate, 0.0075))  # 平稳状态下增长率\n",
    "eq_list0.append(Eq(number_of_capital_firm_workers, 1000))  # 资本品生产部门工人数量\n",
    "eq_list0.append(Eq(households_wage, 5))  # 工人工资\n",
    "eq_list0.append(Eq(mark_up_of_capital, 0.075))  # 资本品加价比例\n",
    "eq_list0.append(Eq(ratio_of_deposits_to_wage, 1))\n",
    "eq_list0.append(Eq(capital_of_consumption_firm, 40000))\n",
    "eq_list0.append(Eq(duration_or_capital, 20))\n",
    "eq_list0.append(Eq(ratio_of_inventory_to_output, 0.1))\n",
    "eq_list0.append(Eq(interest_rate_of_deposits, 0.0025))\n",
    "eq_list0.append(Eq(interest_rate_of_loans, 0.0075))\n",
    "eq_list0.append(Eq(tax_rate, 0.18))\n",
    "eq_list0.append(Eq(ratio_of_dividends_to_profits_left, 0.9))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "一共有未知数30个, 方程30个\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'steady_state_rate': 0.00750000000000000,\n",
       " 'number_of_capital_firm_workers': 1000.00000000000,\n",
       " 'output_of_capital': 2000.00000000000,\n",
       " 'labor_productivity': 2.00000000000000,\n",
       " 'unit_cost_of_capital_firm': 2.50000000000000,\n",
       " 'households_wage': 5.00000000000000,\n",
       " 'price_of_capital': 2.68750000000000,\n",
       " 'mark_up_of_capital': 0.0750000000000000,\n",
       " 'deposits_capital_firms_holds': 5000.00000000000,\n",
       " 'ratio_of_deposits_to_wage': 1.00000000000000,\n",
       " 'capital_of_consumption_firm': 40000.0000000000,\n",
       " 'duration_or_capital': 20.0000000000000,\n",
       " 'capital_firm_inventory': 200.000000000000,\n",
       " 'ratio_of_inventory_to_output': 0.100000000000000,\n",
       " 'profits_of_capital_firm_before_tax': 381.466344464447,\n",
       " 'interest_rate_of_deposits': 0.00250000000000000,\n",
       " 'interest_rate_of_loans': 0.00750000000000000,\n",
       " 'loans_of_capital_firms': 1298.02106027596,\n",
       " 'deposits_interests_of_capital_firm': 12.4069478908189,\n",
       " 'loan_interests_of_capital_firm': 9.66268779361758,\n",
       " 'change_inventory_of_capital_firm': 3.72208436724566,\n",
       " 'tax_of_capital_firms': 68.6639420036005,\n",
       " 'profits_of_capital_firm_after_tax': 312.802402460847,\n",
       " 'tax_rate': 0.180000000000000,\n",
       " 'dividends_of_capital_firms': 281.522162214762,\n",
       " 'ratio_of_dividends_to_profits_left': 0.900000000000000,\n",
       " 'delta_loans_of_capital_firms': 9.66268779361758,\n",
       " 'oo1_steady_state_rate': 0.992555831265509,\n",
       " 'oo2_steady_state_rate': 0.00744416873449132,\n",
       " 'nominal_output_of_capital': 5375.00000000000}"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "variables = [v[0] for k, v in variable_collector0.items()]\n",
    "print(f\"一共有未知数{len(variables)}个, 方程{len(eq_list0)}个\")\n",
    "results = solve(eq_list0, variables)\n",
    "solved_variables = dict(zip([k for k, v in variable_collector0.items()], results[0]))\n",
    "SET_VARIABLES = {k:v for k,v in solved_variables.items() if variable_collector0[k][2] == '外生设定'}\n",
    "solved_variables"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 公式11\n",
    "$N_c = \\frac{{y_c}}{\\mu_K{l_K}}$ <br>\n",
    "$N_c$ number_of_consumption_firm_workers <br>\n",
    "$y_c$ output_of_consumption <br>\n",
    "$\\mu_{k}$ capital_productivity <br>\n",
    "$l_K$ labor_capital_ratio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [],
   "source": [
    "eq_list = []\n",
    "number_of_consumption_firm_workers = Symbol('number_of_consumption_firm_workers')\n",
    "output_of_consumption = Symbol('output_of_consumption')\n",
    "capital_productivity = Symbol('capital_productivity')\n",
    "labor_capital_ratio = Symbol('labor_capital_ratio')\n",
    "eq_list.append(\n",
    "    Eq(number_of_consumption_firm_workers, output_of_consumption / (capital_productivity * labor_capital_ratio))\n",
    ")\n",
    "# 4个未知数，1个方程"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 公式12"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "${uvc_c} = \\frac{WN_c}{{y_c}}$ <br>\n",
    "$uvc_c$ unit_cost_of_consumption_firm 消费品公司单位成本\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [],
   "source": [
    "unit_variable_cost_of_consumption_firm = Symbol('unit_variable_cost_of_consumption_firm')\n",
    "eq_list.append(\n",
    "    Eq(unit_variable_cost_of_consumption_firm, households_wage * number_of_consumption_firm_workers / output_of_consumption)\n",
    ")\n",
    "# 5个未知数， 2个方程"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 公式13"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "因为我们在一个真正的静止状态，假设资本持续$\\kappa$时期，<br>\n",
    "消费公司投资在每个时期购买数量为$k$的资本货物为了取代一批过时的资本 <br>\n",
    "从而保持总资本$K$不变。<br>\n",
    "由于我们进一步假设了线性的财务摊销，消费品公司就会提取$\\frac{1}{\\kappa}$的历史上每期购买的资本 <br>\n",
    "历史上每期购买的资本表示为$\\frac{p_k}{\\kappa}\\frac{k}{(1+g_{ss})^j}$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$uc_c = \\frac{WN_c + \\frac{p_k·k}{\\kappa^2}\\sum_{j=1}^{\\kappa}\\frac{1}{(1+g_{ss})^j}}{y_c}$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "为了求解方便，我们先求这个$\\sum_{j=1}^{\\kappa}\\frac{1}{(1+g_{ss})^j}$,我们定义为supp_para_for_consumption_unit_cost"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [],
   "source": [
    "unit_cost_of_consumption_firm = Symbol('unit_cost_of_consumption_firm')\n",
    "nominal_output_of_consumpiton = Symbol('nominal_output_of_consumption')\n",
    "supp_para_for_consumption_unit_cost = Symbol(\"supp_para_for_consumption_unit_cost\")\n",
    "\n",
    "eq_list.append(\n",
    "    Eq(unit_cost_of_consumption_firm, \n",
    "       (households_wage * number_of_consumption_firm_workers + supp_para_for_consumption_unit_cost * price_of_capital * \n",
    "        capital_of_consumption_firm / (duration_or_capital **2)) / output_of_consumption)\n",
    ")\n",
    "# 8个未知数，3个方程"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 公式14"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$K_c = \\frac{p_k·k}{\\kappa^2}{\\sum_{j=1}^\\kappa\\frac{j}{(1+g_{ss})^{CL-j}}}$ <br>\n",
    "这里我们提取${\\sum_{j=1}^{\\kappa}\\frac{j}{(1+g_{ss})^{CL-j}}}$作为辅助参数 <br>\n",
    "supp_para_for_stock_of_capital"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [],
   "source": [
    "supp_para_for_stock_of_capital = Symbol(\"supp_para_for_stock_of_capital\")\n",
    "stock_of_capital_in_consumption_firm = Symbol(\"stock_of_capital_in_consumption_firm\")\n",
    "eq_list.append(\n",
    "    Eq(stock_of_capital_in_consumption_firm,\n",
    "       supp_para_for_stock_of_capital * price_of_capital * capital_of_consumption_firm / (duration_or_capital **2))\n",
    "              )\n",
    "# 10个未知数，4个方程"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 公式15"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$p_c = (1+mu_c)·uvc_c$ <br>\n",
    "消费品价格等于其可变生产成本的加成"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [],
   "source": [
    "mark_up_of_consumption = Symbol(\"mark_up_of_consumption\")\n",
    "price_of_consumption = Symbol(\"price_of_consumption\")\n",
    "\n",
    "eq_list.append(\n",
    "    Eq(price_of_consumption, (1 + mark_up_of_consumption) * unit_variable_cost_of_consumption_firm)\n",
    ")\n",
    "eq_list.append(\n",
    "    Eq(nominal_output_of_consumpiton, output_of_consumption * price_of_consumption)\n",
    ")\n",
    "# 12 个未知数 6个方程"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 公式16"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$D_c = \\sigma{WN_c}$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "公司持有的存款数量等比例于所发工资数量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [],
   "source": [
    "deposits_consumption_firm_holds = Symbol('deposits_consumption_firm_holds')\n",
    "eq_list.append(\n",
    "    Eq(deposits_consumption_firm_holds, ratio_of_deposits_to_wage * households_wage * number_of_consumption_firm_workers)\n",
    ")\n",
    "# 13个未知数 7个方程"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 公式17"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$y_c = ku\\mu_K$ 产出等于存量资本乘以资本利用率，乘以产能利用率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [],
   "source": [
    "capacity_of_utilization_of_capital = Symbol(\"capacity_of_utilization_of_capital\")\n",
    "eq_list.append(\n",
    "    Eq(output_of_consumption, capital_of_consumption_firm * capacity_of_utilization_of_capital * capital_productivity)\n",
    ")\n",
    "# 14个未知数, 8个方程"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 公式18\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "${inv_c} = \\nu{y_c}$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "存货等于产出的比例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [],
   "source": [
    "consumption_firm_inventory = Symbol(\"consumption_firm_inventory\")\n",
    "eq_list.append(\n",
    "    Eq(consumption_firm_inventory, ratio_of_inventory_to_output * output_of_consumption)\n",
    ")\n",
    "# 15个未知数，9个方程"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 公式19"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$\\pi_c = p_cy_c + \\frac{i^dD_c}{1+g_{ss}} + {inv_c}\\frac{g_{ss}}{1+g_{ss}}{uc_c} - WN_c - \\frac{i^l{L_c}}{1+g_{ss}} - \\frac{p_k·k}{\\kappa^2}{\\sum_{j=1}^k\\frac{1}{(1+g_{ss})^j}}$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "公司利润等于 <br>\n",
    "+产品销售额 <br>\n",
    "+存款利息<br>\n",
    "+存货变动<br>\n",
    "-员工工资 <br>\n",
    "-债务利息 <br>\n",
    "-折旧 <br>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [],
   "source": [
    "profits_of_consumption_firm = Symbol(\"profits_of_consumption_firm\")\n",
    "loans_of_consumption_firms = Symbol(\"loans_of_consumption_firms\")\n",
    "\n",
    "deposits_interests_of_consumption_firm = Symbol('deposits_interests_of_consumption_firm')\n",
    "\n",
    "\n",
    "amortization_of_consumption_firms = Symbol(\"amortization_of_consumption_firms\")\n",
    "eq_list.append(\n",
    "    Eq(amortization_of_consumption_firms, \n",
    "       supp_para_for_consumption_unit_cost * price_of_capital * capital_of_consumption_firm / (duration_or_capital **2)\n",
    "))\n",
    "eq_list.append(\n",
    "    Eq(deposits_interests_of_consumption_firm, interest_rate_of_deposits * deposits_consumption_firm_holds * oo1_steady_state_rate)\n",
    ")\n",
    "\n",
    "\n",
    "eq_list.append(\n",
    "    Eq(profits_of_consumption_firm, \n",
    "       price_of_consumption * output_of_consumption + \n",
    "       deposits_interests_of_consumption_firm +\n",
    "       consumption_firm_inventory * unit_cost_of_consumption_firm * oo2_steady_state_rate -\n",
    "       households_wage * number_of_consumption_firm_workers - \n",
    "       interest_rate_of_loans * loans_of_consumption_firms * oo1_steady_state_rate - \n",
    "       amortization_of_consumption_firms\n",
    "      )\n",
    ")\n",
    "# 19个未知数 12个方程"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [],
   "source": [
    "# if 'output_of_consumption' in solved_variables:\n",
    "#     sv = solved_variables\n",
    "#     data = {\n",
    "#         '消费品公司价格': sv['price_of_consumption'],\n",
    "#         '消费品公司产出': sv['output_of_consumption'],\n",
    "#     }\n",
    "# solved_variables"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 公式20"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "${T_c} = \\pi_c·{\\tau_c}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [],
   "source": [
    "tax_of_consumption_firms = Symbol('tax_of_consumption_firms')\n",
    "eq_list.append(\n",
    "    Eq(tax_of_consumption_firms, tax_rate * profits_of_consumption_firm)\n",
    ")\n",
    "# 20个未知数， 13个方程"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 公式21"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "${Div_c} = \\rho_c[\\pi_c·（1-{\\tau_c}）]$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [],
   "source": [
    "dividends_of_consumption_firms = Symbol('dividends_of_consumption_firms')\n",
    "eq_list.append(\n",
    "    Eq(dividends_of_consumption_firms, ratio_of_dividends_to_profits_left * (profits_of_consumption_firm * (1 - tax_rate)\n",
    "                                                                            ))\n",
    ")\n",
    "# 21个未知数， 14个方程"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [],
   "source": [
    "# if 'dividends_of_consumption_firms' in solved_variables:\n",
    "#     sv = solved_variables\n",
    "#     data = {\n",
    "#         '消费品公司股利': sv['dividends_of_consumption_firms'],\n",
    "#         '股利比率': sv['ratio_of_dividends_to_profits_left'],\n",
    "#         '消费品公司利润': sv['profits_of_consumption_firm'],\n",
    "#         '消费品公司交税': sv['tax_of_consumption_firms'],\n",
    "#         '税率': sv['tax_rate'],\n",
    "#     }\n",
    "# data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 公式22"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$L_c(\\frac{g_{ss}}{1+g_{ss}}) = i_cp_k·（{inv_c}\\frac{g_{ss}}{1+g_{ss}}{uc_c}）+ {D_c}(\\frac{g_{ss}}{1+g_{ss}}) \\\\ - ({\\pi_c-T_c-Div_c}) - \\frac{p_k·k}{\\kappa^2}{\\sum_{j=1}^k\\frac{1}{(1+g_{ss})^j}}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [],
   "source": [
    "eq_list.append(\n",
    "    Eq(loans_of_consumption_firms * oo2_steady_state_rate, \n",
    "      output_of_capital * price_of_capital + \n",
    "      consumption_firm_inventory * unit_cost_of_consumption_firm * oo2_steady_state_rate +\n",
    "      deposits_consumption_firm_holds * oo2_steady_state_rate - \n",
    "      (profits_of_consumption_firm - tax_of_consumption_firms - dividends_of_consumption_firms) -\n",
    "      amortization_of_consumption_firms\n",
    "      )\n",
    ")\n",
    "# 21个未知数，15个方程"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数据关联"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [],
   "source": [
    "variable_collector = {\n",
    "    'number_of_consumption_firm_workers': (number_of_consumption_firm_workers, \"消费品公司员工数量\",\"外生设定\"),\n",
    "    'output_of_consumption': (output_of_consumption, \"消费品公司实际产出\",\"-\"),\n",
    "    'capital_productivity': (capital_productivity, \"生产消费品的资本生产率\",\"外生设定\"),\n",
    "    'labor_capital_ratio': (labor_capital_ratio, \"生产消费品的劳动资本比率\",\"外生设定\"),\n",
    "    'unit_variable_cost_of_consumption_firm':(unit_variable_cost_of_consumption_firm,\"消费品单位可变生产成本\",\"-\"), \n",
    "    'unit_cost_of_consumption_firm':(unit_cost_of_consumption_firm,\"消费品单位生产成本(包括折旧)\",\"-\"), \n",
    "    'supp_para_for_consumption_unit_cost':(supp_para_for_consumption_unit_cost, \"用于计算消费品单位生产成本的系数\", \"-\"),\n",
    "    'supp_para_for_stock_of_capital' :(supp_para_for_stock_of_capital, \"用于计算资本存量的系数\", \"-\"),\n",
    "    'stock_of_capital_in_consumption_firm':(stock_of_capital_in_consumption_firm, \"消费品公司资本存量\", \"-\"),\n",
    "    'mark_up_of_consumption':(mark_up_of_consumption, \"资本公司加价比例\", \"外生设定\"),\n",
    "    'price_of_consumption':(price_of_consumption, \"消费品价格\", \"-\"),\n",
    "    'nominal_output_of_consumpiton':(nominal_output_of_consumpiton, \"消费品公司名义产出\", \"-\"),\n",
    "    'deposits_consumption_firm_holds':(deposits_consumption_firm_holds, \"消费品公司存款\", \"-\"),\n",
    "    'deposits_interests_of_consumption_firm':(deposits_interests_of_consumption_firm, \"消费品公司存款利息\", \"-\"),\n",
    "    'amortization_of_consumption_firms':(amortization_of_consumption_firms, \"消费品公司折旧\", \"-\"),\n",
    "    'capacity_of_utilization_of_capital':(capacity_of_utilization_of_capital, \"消费品公司资本利用率\", \"外生设定\"),\n",
    "    'consumption_firm_inventory':(consumption_firm_inventory, \"消费品公司存货\", \"-\"),\n",
    "    'profits_of_consumption_firm':(profits_of_consumption_firm, \"消费品公司利润\", \"-\"),\n",
    "    'loans_of_consumption_firms':(loans_of_consumption_firms, \"消费品公司贷款\", \"-\"),\n",
    "    'tax_of_consumption_firms':(tax_of_consumption_firms, \"消费品公司交税\", \"-\"),\n",
    "    'dividends_of_consumption_firms':(dividends_of_consumption_firms, \"消费品公司股利分配\", \"-\"),\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 参数赋值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [],
   "source": [
    "eq_list.append(Eq(number_of_consumption_firm_workers, 5000))\n",
    "eq_list.append(Eq(capital_productivity, 1))\n",
    "# eq_list.append(Eq(labor_capital_ratio, 6.4))\n",
    "eq_list.append(Eq(supp_para_for_consumption_unit_cost,sum([(1.0 / (1.0 + steady_state_rate))**i for i in range(1,21)])))\n",
    "eq_list.append(Eq(supp_para_for_stock_of_capital, sum([i / (1 + steady_state_rate) ** (20 - i) for i in range(1,21)])))\n",
    "eq_list.append(Eq(mark_up_of_consumption, 0.319))\n",
    "eq_list.append(Eq(capacity_of_utilization_of_capital, 0.8))\n",
    "# 21个未知数（6个参数）， 15个方程"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 解方程"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "一共有未知数51个, 方程51个\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'number_of_consumption_firm_workers': 5000.00000000000,\n",
       " 'output_of_consumption': 32000.0000000000,\n",
       " 'capital_productivity': 1.00000000000000,\n",
       " 'labor_capital_ratio': 6.40000000000000,\n",
       " 'unit_variable_cost_of_consumption_firm': 0.781250000000000,\n",
       " 'unit_cost_of_consumption_firm': 0.936688446646308,\n",
       " 'supp_para_for_consumption_unit_cost': 18.5080196937000,\n",
       " 'supp_para_for_stock_of_capital': 200.422687812969,\n",
       " 'stock_of_capital_in_consumption_firm': 53863.5973497354,\n",
       " 'mark_up_of_consumption': 0.319000000000000,\n",
       " 'price_of_consumption': 1.03046875000000,\n",
       " 'nominal_output_of_consumpiton': 32975.0000000000,\n",
       " 'deposits_consumption_firm_holds': 25000.0000000000,\n",
       " 'deposits_interests_of_consumption_firm': 62.0347394540943,\n",
       " 'amortization_of_consumption_firms': 4974.03029268187,\n",
       " 'capacity_of_utilization_of_capital': 0.800000000000000,\n",
       " 'consumption_firm_inventory': 3200.00000000000,\n",
       " 'profits_of_consumption_firm': 2697.09207090611,\n",
       " 'loans_of_consumption_firms': 52151.6321872825,\n",
       " 'tax_of_consumption_firms': 485.476572763100,\n",
       " 'dividends_of_consumption_firms': 1990.45394832871}"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "for k,v in solved_variables.items():\n",
    "    eq_list.append(Eq(variable_collector0[k][0], float(v)))\n",
    "variable_collector1 = {**variable_collector, **variable_collector0}\n",
    "variables = [v[0] for k, v in variable_collector1.items()]\n",
    "print(f\"一共有未知数{len(variables)}个, 方程{len(eq_list)}个\")\n",
    "results = solve(eq_list, variables)\n",
    "solved_variables = dict(zip([k for k, v in variable_collector1.items()], results[0]))\n",
    "solved_variables = {k:v for k,v in solved_variables.items()}\n",
    "solved_variables_output = {k:v for k,v in solved_variables.items() if k in variable_collector}\n",
    "solved_variables_output"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>参数中文释义</th>\n",
       "      <th>设定情况</th>\n",
       "      <th>解</th>\n",
       "      <th>para</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>消费品公司员工数量</td>\n",
       "      <td>外生设定</td>\n",
       "      <td>5000.000000</td>\n",
       "      <td>number_of_consumption_firm_workers</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>消费品公司实际产出</td>\n",
       "      <td>-</td>\n",
       "      <td>32000.000000</td>\n",
       "      <td>output_of_consumption</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>生产消费品的资本生产率</td>\n",
       "      <td>外生设定</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>capital_productivity</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>生产消费品的劳动资本比率</td>\n",
       "      <td>外生设定</td>\n",
       "      <td>6.400000</td>\n",
       "      <td>labor_capital_ratio</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>消费品单位可变生产成本</td>\n",
       "      <td>-</td>\n",
       "      <td>0.781250</td>\n",
       "      <td>unit_variable_cost_of_consumption_firm</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>消费品单位生产成本(包括折旧)</td>\n",
       "      <td>-</td>\n",
       "      <td>0.936688</td>\n",
       "      <td>unit_cost_of_consumption_firm</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>用于计算消费品单位生产成本的系数</td>\n",
       "      <td>-</td>\n",
       "      <td>18.508020</td>\n",
       "      <td>supp_para_for_consumption_unit_cost</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>用于计算资本存量的系数</td>\n",
       "      <td>-</td>\n",
       "      <td>200.422688</td>\n",
       "      <td>supp_para_for_stock_of_capital</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>消费品公司资本存量</td>\n",
       "      <td>-</td>\n",
       "      <td>53863.597350</td>\n",
       "      <td>stock_of_capital_in_consumption_firm</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>资本公司加价比例</td>\n",
       "      <td>外生设定</td>\n",
       "      <td>0.319000</td>\n",
       "      <td>mark_up_of_consumption</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>消费品价格</td>\n",
       "      <td>-</td>\n",
       "      <td>1.030469</td>\n",
       "      <td>price_of_consumption</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>消费品公司名义产出</td>\n",
       "      <td>-</td>\n",
       "      <td>32975.000000</td>\n",
       "      <td>nominal_output_of_consumpiton</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>消费品公司存款</td>\n",
       "      <td>-</td>\n",
       "      <td>25000.000000</td>\n",
       "      <td>deposits_consumption_firm_holds</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>消费品公司存款利息</td>\n",
       "      <td>-</td>\n",
       "      <td>62.034739</td>\n",
       "      <td>deposits_interests_of_consumption_firm</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>消费品公司折旧</td>\n",
       "      <td>-</td>\n",
       "      <td>4974.030293</td>\n",
       "      <td>amortization_of_consumption_firms</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>消费品公司资本利用率</td>\n",
       "      <td>外生设定</td>\n",
       "      <td>0.800000</td>\n",
       "      <td>capacity_of_utilization_of_capital</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>消费品公司存货</td>\n",
       "      <td>-</td>\n",
       "      <td>3200.000000</td>\n",
       "      <td>consumption_firm_inventory</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>消费品公司利润</td>\n",
       "      <td>-</td>\n",
       "      <td>2697.092071</td>\n",
       "      <td>profits_of_consumption_firm</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>消费品公司贷款</td>\n",
       "      <td>-</td>\n",
       "      <td>52151.632187</td>\n",
       "      <td>loans_of_consumption_firms</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>消费品公司交税</td>\n",
       "      <td>-</td>\n",
       "      <td>485.476573</td>\n",
       "      <td>tax_of_consumption_firms</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>消费品公司股利分配</td>\n",
       "      <td>-</td>\n",
       "      <td>1990.453948</td>\n",
       "      <td>dividends_of_consumption_firms</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              参数中文释义  设定情况             解  \\\n",
       "0          消费品公司员工数量  外生设定   5000.000000   \n",
       "1          消费品公司实际产出     -  32000.000000   \n",
       "2        生产消费品的资本生产率  外生设定      1.000000   \n",
       "3       生产消费品的劳动资本比率  外生设定      6.400000   \n",
       "4        消费品单位可变生产成本     -      0.781250   \n",
       "5    消费品单位生产成本(包括折旧)     -      0.936688   \n",
       "6   用于计算消费品单位生产成本的系数     -     18.508020   \n",
       "7        用于计算资本存量的系数     -    200.422688   \n",
       "8          消费品公司资本存量     -  53863.597350   \n",
       "9           资本公司加价比例  外生设定      0.319000   \n",
       "10             消费品价格     -      1.030469   \n",
       "11         消费品公司名义产出     -  32975.000000   \n",
       "12           消费品公司存款     -  25000.000000   \n",
       "13         消费品公司存款利息     -     62.034739   \n",
       "14           消费品公司折旧     -   4974.030293   \n",
       "15        消费品公司资本利用率  外生设定      0.800000   \n",
       "16           消费品公司存货     -   3200.000000   \n",
       "17           消费品公司利润     -   2697.092071   \n",
       "18           消费品公司贷款     -  52151.632187   \n",
       "19           消费品公司交税     -    485.476573   \n",
       "20         消费品公司股利分配     -   1990.453948   \n",
       "\n",
       "                                      para  \n",
       "0       number_of_consumption_firm_workers  \n",
       "1                    output_of_consumption  \n",
       "2                     capital_productivity  \n",
       "3                      labor_capital_ratio  \n",
       "4   unit_variable_cost_of_consumption_firm  \n",
       "5            unit_cost_of_consumption_firm  \n",
       "6      supp_para_for_consumption_unit_cost  \n",
       "7           supp_para_for_stock_of_capital  \n",
       "8     stock_of_capital_in_consumption_firm  \n",
       "9                   mark_up_of_consumption  \n",
       "10                    price_of_consumption  \n",
       "11           nominal_output_of_consumpiton  \n",
       "12         deposits_consumption_firm_holds  \n",
       "13  deposits_interests_of_consumption_firm  \n",
       "14       amortization_of_consumption_firms  \n",
       "15      capacity_of_utilization_of_capital  \n",
       "16              consumption_firm_inventory  \n",
       "17             profits_of_consumption_firm  \n",
       "18              loans_of_consumption_firms  \n",
       "19                tax_of_consumption_firms  \n",
       "20          dividends_of_consumption_firms  "
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "output_solved_v = {k:(variable_collector1[k][1],variable_collector1[k][2],f\"{v:.6f}\") for k,v in solved_variables.items() if k in variable_collector}\n",
    "df = pd.DataFrame(output_solved_v).T\n",
    "df['para'] = df.index\n",
    "df.index = list(range(len(df)))\n",
    "df.columns = ['参数中文释义', '设定情况', \"解\", \"para\"]\n",
    "df_para = df[df['设定情况']=='外生设定']\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 公式23\n",
    "$N_{tot} = N_c + N_k + N_g$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$\\alpha$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [],
   "source": [
    "number_of_government_workers = Symbol('number_of_government_workers')\n",
    "number_of_total_employment_workers = Symbol('number_of_total_employment_workers')\n",
    "eq_list2 = []\n",
    "eq_list2.append(\n",
    "    Eq(number_of_total_employment_workers, number_of_capital_firm_workers + number_of_consumption_firm_workers + number_of_government_workers)\n",
    ")\n",
    "# 变量2 ，公式1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 公式24-25\n",
    "$NI_h = (WN_{tot} + \\frac{i^dD_h}{1 + g_{ss}} + \\frac{Div_c + Div_k + Div_b}{1 + g_{ss}})(1-\\tau_h)- \\omega(SIZE_{\\phi_{H}}-N_{tot})$ <br>\n",
    "$T_h = (WN_{tot} + \\frac{i^dD_h}{1 + g_{ss}} + \\frac{Div_c + Div_k + Div_b}{1 + g_{ss}})(\\tau_h)$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [],
   "source": [
    "number_of_households = Symbol('number_of_households')\n",
    "ratio_of_dole_to_wage = Symbol('ratio_of_dole_to_wage')\n",
    "dividends_of_banks = Symbol('dividends_of_banks')\n",
    "net_income_of_households = Symbol('net_income_of_households')\n",
    "\n",
    "tax_of_households = Symbol('tax_of_households')\n",
    "wages_of_households = Symbol('wages_of_households')\n",
    "deposits_of_households = Symbol('deposits_of_households')\n",
    "deposits_interests_of_households = Symbol('deposits_interests_of_households')\n",
    "dividends_of_households = Symbol('dividends_of_households')\n",
    "households_income_before_tax_and_dole = Symbol('households_income_before_tax_and_dole')\n",
    "doles_of_households = Symbol('doles_of_households')\n",
    "\n",
    "eq_list2.append(\n",
    "    Eq(wages_of_households, households_wage * number_of_total_employment_workers)\n",
    ")\n",
    "eq_list2.append(\n",
    "    Eq(deposits_interests_of_households, deposits_of_households * interest_rate_of_deposits * oo1_steady_state_rate)\n",
    ")\n",
    "eq_list2.append(\n",
    "    Eq(dividends_of_households, dividends_of_capital_firms + dividends_of_consumption_firms + dividends_of_banks)\n",
    ")\n",
    "eq_list2.append(\n",
    "    Eq(households_income_before_tax_and_dole, wages_of_households + deposits_interests_of_households + dividends_of_households * oo1_steady_state_rate)\n",
    ")\n",
    "# eq 25\n",
    "eq_list2.append(\n",
    "    Eq(tax_of_households, households_income_before_tax_and_dole * tax_rate)\n",
    ")\n",
    "eq_list2.append(\n",
    "    Eq(doles_of_households, ratio_of_dole_to_wage * households_wage * (number_of_households - number_of_total_employment_workers))\n",
    ")\n",
    "# eq 24\n",
    "eq_list2. append(\n",
    "    Eq(net_income_of_households, \n",
    "       households_income_before_tax_and_dole - tax_of_households + doles_of_households\n",
    "       )\n",
    ")\n",
    "# 变量13，公式8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [],
   "source": [
    "# sv = solved_variables\n",
    "# if 'doles_of_households' in sv:\n",
    "#     dd = {'居民工资': sv['wages_of_households'],\n",
    "#          '居民救济金': sv['doles_of_households'],\n",
    "#          '居民存款利息': sv['deposits_interests_of_households'],\n",
    "#          '居民收到股利': sv['dividends_of_households'],\n",
    "#          '消费品公司股利': sv['dividends_of_consumption_firms'],\n",
    "#          '资本品公司股利': sv['dividends_of_capital_firms'],\n",
    "#          '银行股利': sv['dividends_of_banks'],\n",
    "#          }\n",
    "# dd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 公式26\n",
    "$c_h = \\alpha_1\\frac{NI_h}{p_c} + \\alpha_2\\frac{NW_h}{(1 + g_{ss})p_c}$ "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [],
   "source": [
    "real_households_consumption = Symbol('real_households_consumption')\n",
    "net_wealth_of_households = Symbol('net_wealth_of_households')\n",
    "households_consumption_para_1 = Symbol('households_consumption_para_1')\n",
    "households_consumption_para_2 = Symbol('households_consumption_para_2')\n",
    "\n",
    "eq_list2.append(\n",
    "    Eq(real_households_consumption, \n",
    "       households_consumption_para_1 * net_income_of_households / price_of_consumption +\n",
    "       households_consumption_para_2 * oo1_steady_state_rate * net_wealth_of_households / price_of_consumption\n",
    "      )\n",
    ")\n",
    "# 变量17，公式9"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 公式27-28 \n",
    "$c_h = y_c$ <br>\n",
    "$C_h = c_h  p_c$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [],
   "source": [
    "eq_list2.append(Eq(real_households_consumption, output_of_consumption))\n",
    "nominal_households_consumption = Symbol('nominal_households_consumption')\n",
    "eq_list2.append(Eq(nominal_households_consumption, real_households_consumption * price_of_consumption))\n",
    "# 变量18，公式11"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 公式29-30\n",
    "$NW_h(\\frac{g_{ss}}{1+g_{ss}}) = NI_h - C_h - \\frac{Div_c + Div_k + Div_b}{1 + g_{ss}} + (Div_c + Div_k + Div_b) $ <br>\n",
    "$NW_h = D_h$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [],
   "source": [
    "eq_list2.append(\n",
    "    Eq(net_wealth_of_households * oo2_steady_state_rate, \n",
    "      net_income_of_households - nominal_households_consumption - dividends_of_households * oo1_steady_state_rate + dividends_of_households\n",
    "      )\n",
    ")\n",
    "eq_list2.append(Eq(net_wealth_of_households, deposits_of_households))\n",
    "# 变量18 ，公式13"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "17\n",
      "68\n"
     ]
    }
   ],
   "source": [
    "eq_list2.append(Eq(number_of_government_workers, 1360))\n",
    "eq_list2.append(Eq(number_of_households, 8000))\n",
    "eq_list2.append(Eq(ratio_of_dole_to_wage, 0.4))\n",
    "eq_list2.append(Eq(households_consumption_para_2, 0.25))\n",
    "print(len(eq_list2))\n",
    "for k,v in solved_variables.items():\n",
    "    eq_list2.append(Eq(variable_collector1[k][0], float(v)))\n",
    "    # print(k, '赋值', v)\n",
    "print(len(eq_list2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [],
   "source": [
    "variable_collector2 = {\n",
    "    'number_of_government_workers':(number_of_government_workers, \"政府雇员数量\", \"外生设定\"),\n",
    "    'number_of_total_employment_workers':(number_of_total_employment_workers, \"总就业人员数量\", \"-\"),\n",
    "    'number_of_households':(number_of_households, \"居民总量\",\"外生设定\"),\n",
    "    'ratio_of_dole_to_wage':(ratio_of_dole_to_wage, '救济金与普通工资的比例', \"外生设定\"),\n",
    "    'dividends_of_banks':(dividends_of_banks, \"银行股利\", \"-\"),\n",
    "    'net_income_of_households':(net_income_of_households, \"居民总收入\", \"-\"),\n",
    "    'tax_of_households':(tax_of_households, \"居民纳税数量\", \"-\"),\n",
    "    'wages_of_households':(wages_of_households, \"就业群体收到工资额\", '-'),\n",
    "    'doles_of_households':(doles_of_households, \"失业人员救济金\", '-'),\n",
    "    'deposits_of_households':(deposits_of_households, \"居民存款\", '-'),\n",
    "    'deposits_interests_of_households':(deposits_interests_of_households, \"居民存款利息\", '-'),\n",
    "    'dividends_of_households': (dividends_of_households, \"居民收到股利\", '-'),\n",
    "    'households_income_before_tax_and_dole':(households_income_before_tax_and_dole, \"就业居民税前收入\", '-'),\n",
    "    'households_consumption_para_1':(households_consumption_para_1, \"居民部门关于收入的消费倾向\", '-'),\n",
    "    'households_consumption_para_2':(households_consumption_para_2, \"居民部门关于财富的消费倾向\", '外生设定'),\n",
    "    'real_households_consumption':(real_households_consumption, \"居民部门实际消费\", '-'),\n",
    "    'net_wealth_of_households':(net_wealth_of_households, \"居民财富\", '-'),\n",
    "    'nominal_households_consumption':(nominal_households_consumption, \"居民部门名义消费\", '-'),\n",
    "}\n",
    "variable_collector3 = {**variable_collector2, **variable_collector1}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第二次解方程"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [],
   "source": [
    "# variables3 = [v[0] for k, v in variable_collector3.items()]\n",
    "# print(f\"一共有未知数{len(variables3)}个, 方程{len(eq_list2)}个\")\n",
    "\n",
    "# results = solve(eq_list2, variables3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [],
   "source": [
    "# solved_variables = dict(zip([k for k, v in variable_collector3.items()], results[0]))\n",
    "# solved_variables = {k:v for k,v in solved_variables.items()}\n",
    "# solved_variables_output = {k:v for k,v in solved_variables.items() if k in variable_collector2}\n",
    "# solved_variables_output"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 公式31-33\n",
    "$\\pi_b = i^l \\frac{L_c + L_k}{1 + g_{ss}} + i^b \\frac{B_b}{1 + g_{ss}} - i^d\\frac{D_h + D_c + D_k}{1 + g_{ss}}$ 银行利润计算 <br>\n",
    "$T_b = \\pi_b\\tau_b$ 银行税收<br>\n",
    "$Div_b = \\rho_b[\\pi_b(1-\\tau_b)]$ 银行股利"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [],
   "source": [
    "# print(solved_variables['profits_of_bank_before_tax'])\n",
    "# print(solved_variables['loans_of_consumption_firms'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {},
   "outputs": [],
   "source": [
    "profits_of_bank_before_tax = Symbol('profits_of_bank_before_tax')\n",
    "bank_receive_loans_interests = Symbol('bank_receive_loans_interests')\n",
    "interest_rate_of_bonds = Symbol('interest_rate_of_bonds')\n",
    "bank_receive_bonds_interests = Symbol('bank_receive_bonds_interests')\n",
    "bank_hold_bonds = Symbol('bank_hold_bonds')\n",
    "bank_pay_deposits_interests = Symbol('bank_pay_deposits_interests')\n",
    "\n",
    "ratio_of_dividends_to_profits_left_in_bank = Symbol('ratio_of_dividends_to_profits_left_in_bank')\n",
    "tax_of_bank = Symbol('tax_of_bank')\n",
    "\n",
    "eq_list3 = []\n",
    "loan_interests_of_consumption_firm = Symbol('loan_interests_of_consumption_firm')\n",
    "eq_list3.append(\n",
    "    Eq(loan_interests_of_consumption_firm, interest_rate_of_loans * loans_of_consumption_firms * oo1_steady_state_rate)\n",
    ")\n",
    "eq_list3.append(\n",
    "    Eq(bank_receive_loans_interests, loan_interests_of_capital_firm + loan_interests_of_consumption_firm)\n",
    ")\n",
    "eq_list3.append(\n",
    "    Eq(bank_receive_bonds_interests, interest_rate_of_bonds * bank_hold_bonds * oo1_steady_state_rate)\n",
    ")\n",
    "eq_list3.append(\n",
    "    Eq(bank_pay_deposits_interests, \n",
    "       deposits_interests_of_capital_firm + deposits_interests_of_consumption_firm + deposits_interests_of_households)\n",
    ")\n",
    "eq_list3.append(\n",
    "    Eq(profits_of_bank_before_tax, bank_receive_loans_interests + bank_receive_bonds_interests - bank_pay_deposits_interests)\n",
    ")\n",
    "eq_list3.append(\n",
    "    Eq(tax_of_bank, profits_of_bank_before_tax * tax_rate)\n",
    ")\n",
    "eq_list3.append(\n",
    "    Eq(dividends_of_banks, ratio_of_dividends_to_profits_left_in_bank * (profits_of_bank_before_tax - tax_of_bank))\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {},
   "outputs": [],
   "source": [
    "# print(f\"银行收到贷款利息{solved_variables['bank_receive_loans_interests']:.2f}=\")\n",
    "# print(f\"银行支付存款利息{solved_variables['bank_pay_deposits_interests']:.2f}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 公式34-36\n",
    "$NW_b = L_c + L_k + B_b + R_b - D_c - D_k - D_h$ <br>\n",
    "$R_b = B_{cb}$ 银行的存款准备金等于中央银行持有的政府债券 <br>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {},
   "outputs": [],
   "source": [
    "# interest_rate_of_reserves = Symbol('interest_rate_of_reserves')\n",
    "bank_reserves = Symbol('bank_reserves')\n",
    "net_wealth_of_bank = Symbol('net_wealth_of_bank')\n",
    "central_bank_hold_bonds = Symbol('central_bank_hold_bonds')\n",
    "\n",
    "bonds_government_issue = Symbol('bonds_government_issue')\n",
    "\n",
    "eq_list3.append(\n",
    "    Eq(net_wealth_of_bank, \n",
    "       loans_of_capital_firms + loans_of_consumption_firms + bank_hold_bonds + bank_reserves - \n",
    "       deposits_capital_firms_holds - deposits_consumption_firm_holds - deposits_of_households\n",
    "      )\n",
    ")\n",
    "eq_list3.append(\n",
    "    Eq(bank_reserves, central_bank_hold_bonds)\n",
    ")\n",
    "eq_list3.append(\n",
    "    Eq(central_bank_hold_bonds, bonds_government_issue - bank_hold_bonds)\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {},
   "outputs": [],
   "source": [
    "# print(f\"银行净值{solved_variables['net_wealth_of_bank']:.2f}=\")\n",
    "# print(f\"C公司贷款{solved_variables['loans_of_capital_firms']:.2f}\")\n",
    "# print(f\"+k公司贷款{solved_variables['loans_of_consumption_firms']:.2f}\")\n",
    "# print(f\"-c公司存款{solved_variables['deposits_consumption_firm_holds']:.2f}\")\n",
    "# print(f\"-k公司存款{solved_variables['deposits_capital_firms_holds']:.2f}\")\n",
    "# print(f\"-居民存款{solved_variables['deposits_of_households']:.2f}\")\n",
    "# print(f\"+银行持有债券{solved_variables['bank_hold_bonds']:.2f}\")\n",
    "# print(f\"+银行持有准备金（中央银行持有债券）{solved_variables['bank_reserves']:.2f}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 公式37-38\n",
    "$B(\\frac{g_{ss}}{1+ g_{ss}}) = WN_g +\\omega{W}(SIZW_{\\phi_{H}} - N_{tot}) + \\frac{i^bB}{1 + g_{ss}} - (T_h + T_b + T_c + T_k) - \\pi_{cb}$ <br> \n",
    "政府债务增长 = 雇员支出 + 救济金支出 + 利息支出 - 税收 - 中央银行利润 <br>\n",
    "$\\pi_{cb} = \\frac{i^bB_{cb}}{1 + g_{ss}}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {},
   "outputs": [],
   "source": [
    "government_pay_bonds_interests = Symbol('government_pay_bonds_interests')\n",
    "government_collect_tax = Symbol('government_collect_tax')\n",
    "central_bank_profits = Symbol('central_bank_profits')\n",
    "\n",
    "eq_list3.append(Eq(government_pay_bonds_interests, interest_rate_of_bonds * bonds_government_issue * oo1_steady_state_rate))\n",
    "eq_list3.append(Eq(government_collect_tax, \n",
    "                  tax_of_bank + tax_of_capital_firms + tax_of_consumption_firms + tax_of_households))\n",
    "eq_list3.append(Eq(central_bank_profits, interest_rate_of_bonds * central_bank_hold_bonds * oo1_steady_state_rate))\n",
    "eq_list3.append(\n",
    "    Eq(bonds_government_issue * oo2_steady_state_rate, \n",
    "      households_wage * number_of_government_workers + \n",
    "      doles_of_households + \n",
    "      government_pay_bonds_interests - \n",
    "      government_collect_tax -\n",
    "      central_bank_profits\n",
    "      )\n",
    ")\n",
    "eq_list3.append(Eq(interest_rate_of_bonds, 0.0025))\n",
    "eq_list3.append(Eq(ratio_of_dividends_to_profits_left_in_bank, 0.6))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 公式40"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$NW_b = \\beta(L_c + L_k + B_b + R_b)$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {},
   "outputs": [],
   "source": [
    "ratio_of_bank_net_wealth_to_assets = Symbol('ratio_of_bank_net_wealth_to_assets')\n",
    "eq_list3.append(Eq(ratio_of_bank_net_wealth_to_assets, 0.08))\n",
    "eq_list3.append(\n",
    "    Eq(net_wealth_of_bank, \n",
    "       ratio_of_bank_net_wealth_to_assets * (loans_of_capital_firms + loans_of_consumption_firms + bank_hold_bonds + bank_reserves))\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {},
   "outputs": [],
   "source": [
    "# print(f\"政府债务增长{solved_variables['bonds_government_issue'] * solved_variables['oo2_steady_state_rate'] :.2f}=\")\n",
    "# print(f\"+雇员支出   {solved_variables['households_wage'] * solved_variables['number_of_government_workers']:.2f}\")\n",
    "# print(f\"+救济金支出   {solved_variables['doles_of_households']:.2f}\")\n",
    "# print(f\"+政府债券利息   {solved_variables['government_pay_bonds_interests']:.2f}\")\n",
    "# print(f\"-政府税收   {solved_variables['government_collect_tax']:.2f}\")\n",
    "# print(f\"    居民税收   {solved_variables['tax_of_households']:.2f}\")\n",
    "# print(f\"    消费品公司税收   {solved_variables['tax_of_consumption_firms']:.2f}\")\n",
    "# print(f\"    资本品公司税收   {solved_variables['tax_of_capital_firms']:.2f}\")\n",
    "# print(f\"    银行税收   {solved_variables['tax_of_bank']:.2f}\")\n",
    "# print(f\"-中央银行利润   {solved_variables['central_bank_profits']:.2f}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {},
   "outputs": [],
   "source": [
    "eq_list3 = [*eq_list2, *eq_list3]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {},
   "outputs": [],
   "source": [
    "variable_collector4 = {\n",
    "    'profits_of_bank_before_tax':(profits_of_bank_before_tax, \"银行税前利润\", \"-\"),\n",
    "    'bank_receive_loans_interests':(bank_receive_loans_interests, \"银行收到贷款利息\", \"-\"),\n",
    "    'interest_rate_of_bonds':(interest_rate_of_bonds, \"债券利息\",\"外生设定\"),\n",
    "    'bank_receive_bonds_interests':(bank_receive_bonds_interests, '银行收到债券利息', \"-\"),\n",
    "    'bank_hold_bonds':(bank_hold_bonds, \"银行持有债券\", \"-\"),\n",
    "    'bank_pay_deposits_interests':(bank_pay_deposits_interests, \"银行支付存款利息\", \"-\"),\n",
    "    'tax_of_bank':(tax_of_bank, \"银行纳税\", \"-\"),\n",
    "    'bank_reserves':(bank_reserves, \"银行准备金\", '-'),\n",
    "    'net_wealth_of_bank':(net_wealth_of_bank, \"银行净值\", '-'),\n",
    "    'central_bank_hold_bonds':(central_bank_hold_bonds, \"中央银行持有债券\", '-'),\n",
    "    'bonds_government_issue': (bonds_government_issue, \"政府发行债券\", '-'),\n",
    "    'government_pay_bonds_interests':(government_pay_bonds_interests, \"政府支付债券利息\", '-'),\n",
    "    'government_collect_tax':(government_collect_tax, \"政府收税金额\", '-'),\n",
    "    'central_bank_profits':(central_bank_profits, \"中央银行利润\", '-'),\n",
    "    'ratio_of_dividends_to_profits_left_in_bank':(ratio_of_dividends_to_profits_left_in_bank, '银行股利比例', '外生设定'),\n",
    "    'ratio_of_bank_net_wealth_to_assets':(ratio_of_bank_net_wealth_to_assets, \"银行净值占资产比例\", \"-\"),\n",
    "    'loan_interests_of_consumption_firm':(loan_interests_of_consumption_firm, \"消费品公司贷款利息\", \"-\"),\n",
    "}\n",
    "variable_collector4 = {**variable_collector3, **variable_collector4}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "一共有未知数86个, 方程86个\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'number_of_government_workers': 1360.00000000000,\n",
       " 'number_of_total_employment_workers': 7360.00000000000,\n",
       " 'number_of_households': 8000.00000000000,\n",
       " 'ratio_of_dole_to_wage': 0.400000000000000,\n",
       " 'dividends_of_banks': 107.236245085391,\n",
       " 'net_income_of_households': 33556.1082253894,\n",
       " 'tax_of_households': 7084.99936654890,\n",
       " 'wages_of_households': 36800.0000000000,\n",
       " 'doles_of_households': 1280.00000000000,\n",
       " 'deposits_of_households': 80441.4172996118,\n",
       " 'deposits_interests_of_households': 199.606494539980,\n",
       " 'dividends_of_households': 2379.21235562886,\n",
       " 'households_income_before_tax_and_dole': 39361.1075919384,\n",
       " 'households_consumption_para_1': 0.387838496007563,\n",
       " 'households_consumption_para_2': 0.250000000000000,\n",
       " 'real_households_consumption': 32000.0000000000,\n",
       " 'net_wealth_of_households': 80441.4172996118,\n",
       " 'nominal_households_consumption': 32975.0000000000,\n",
       " 'number_of_consumption_firm_workers': 5000.00000000000,\n",
       " 'output_of_consumption': 32000.0000000000,\n",
       " 'capital_productivity': 1.00000000000000,\n",
       " 'labor_capital_ratio': 6.40000000000000,\n",
       " 'unit_variable_cost_of_consumption_firm': 0.781250000000000,\n",
       " 'unit_cost_of_consumption_firm': 0.936688446646308,\n",
       " 'supp_para_for_consumption_unit_cost': 18.5080196937000,\n",
       " 'supp_para_for_stock_of_capital': 200.422687812969,\n",
       " 'stock_of_capital_in_consumption_firm': 53863.5973497354,\n",
       " 'mark_up_of_consumption': 0.319000000000000,\n",
       " 'price_of_consumption': 1.03046875000000,\n",
       " 'nominal_output_of_consumpiton': 32975.0000000000,\n",
       " 'deposits_consumption_firm_holds': 25000.0000000000,\n",
       " 'deposits_interests_of_consumption_firm': 62.0347394540943,\n",
       " 'amortization_of_consumption_firms': 4974.03029268187,\n",
       " 'capacity_of_utilization_of_capital': 0.800000000000000,\n",
       " 'consumption_firm_inventory': 3200.00000000000,\n",
       " 'profits_of_consumption_firm': 2697.09207090611,\n",
       " 'loans_of_consumption_firms': 52151.6321872825,\n",
       " 'tax_of_consumption_firms': 485.476572763100,\n",
       " 'dividends_of_consumption_firms': 1990.45394832871,\n",
       " 'steady_state_rate': 0.00750000000000000,\n",
       " 'number_of_capital_firm_workers': 1000.00000000000,\n",
       " 'output_of_capital': 2000.00000000000,\n",
       " 'labor_productivity': 2.00000000000000,\n",
       " 'unit_cost_of_capital_firm': 2.50000000000000,\n",
       " 'households_wage': 5.00000000000000,\n",
       " 'price_of_capital': 2.68750000000000,\n",
       " 'mark_up_of_capital': 0.0750000000000000,\n",
       " 'deposits_capital_firms_holds': 5000.00000000000,\n",
       " 'ratio_of_deposits_to_wage': 1.00000000000000,\n",
       " 'capital_of_consumption_firm': 40000.0000000000,\n",
       " 'duration_or_capital': 20.0000000000000,\n",
       " 'capital_firm_inventory': 200.000000000000,\n",
       " 'ratio_of_inventory_to_output': 0.100000000000000,\n",
       " 'profits_of_capital_firm_before_tax': 381.466344464447,\n",
       " 'interest_rate_of_deposits': 0.00250000000000000,\n",
       " 'interest_rate_of_loans': 0.00750000000000000,\n",
       " 'loans_of_capital_firms': 1298.02106027596,\n",
       " 'deposits_interests_of_capital_firm': 12.4069478908189,\n",
       " 'loan_interests_of_capital_firm': 9.66268779361758,\n",
       " 'change_inventory_of_capital_firm': 3.72208436724566,\n",
       " 'tax_of_capital_firms': 68.6639420036005,\n",
       " 'profits_of_capital_firm_after_tax': 312.802402460846,\n",
       " 'tax_rate': 0.180000000000000,\n",
       " 'dividends_of_capital_firms': 281.522162214762,\n",
       " 'ratio_of_dividends_to_profits_left': 0.900000000000000,\n",
       " 'delta_loans_of_capital_firms': 9.66268779361758,\n",
       " 'oo1_steady_state_rate': 0.992555831265509,\n",
       " 'oo2_steady_state_rate': 0.00744416873449132,\n",
       " 'nominal_output_of_capital': 5375.00000000000,\n",
       " 'profits_of_bank_before_tax': 217.959847734534,\n",
       " 'bank_receive_loans_interests': 397.888237574877,\n",
       " 'interest_rate_of_bonds': 0.00250000000000000,\n",
       " 'bank_receive_bonds_interests': 94.1197920445503,\n",
       " 'bank_hold_bonds': 37930.2761939538,\n",
       " 'bank_pay_deposits_interests': 274.048181884893,\n",
       " 'tax_of_bank': 39.2327725922162,\n",
       " 'bank_reserves': 28665.0893624136,\n",
       " 'net_wealth_of_bank': 9603.60150431407,\n",
       " 'central_bank_hold_bonds': 28665.0893624136,\n",
       " 'bonds_government_issue': 66595.3655563674,\n",
       " 'government_pay_bonds_interests': 165.249046045577,\n",
       " 'government_collect_tax': 7678.37265390782,\n",
       " 'central_bank_profits': 71.1292540010263,\n",
       " 'ratio_of_dividends_to_profits_left_in_bank': 0.600000000000000,\n",
       " 'ratio_of_bank_net_wealth_to_assets': 0.0800000000000000,\n",
       " 'loan_interests_of_consumption_firm': 388.225549781259}"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "variables4 = [v[0] for k, v in variable_collector4.items()]\n",
    "print(f\"一共有未知数{len(variables4)}个, 方程{len(eq_list3)}个\")\n",
    "results = solve(eq_list3, variables4)\n",
    "solved_variables = dict(zip([k for k, v in variable_collector4.items()], results[0]))\n",
    "solved_variables"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {},
   "outputs": [],
   "source": [
    "output_solved_v = {k:(variable_collector4[k][1],variable_collector4[k][2],f\"{v:.6f}\") for k,v in solved_variables.items()}\n",
    "df = pd.DataFrame(output_solved_v).T\n",
    "df['para'] = df.index\n",
    "df.index = list(range(len(df)))\n",
    "df.columns = ['参数中文释义', '设定情况', \"解\", \"para\"]\n",
    "df_para = df[df['设定情况']=='外生设定']\n",
    "df_para.to_excel(\"output/外生设定参数.xlsx\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_excel(\"output/所有参数.xlsx\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "资本品公司存货增加3.722084, check_data3.7, 验证 错误\n",
      "银行收到债券利息94.119792, check_data95, 验证 错误\n",
      "银行持有债券37930.276194, check_data38273.5, 验证 错误\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\59567\\AppData\\Local\\Temp\\ipykernel_27460\\2170489258.py:2: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  df['验证'] = ['-' for i in range(len(df))]\n",
      "C:\\Users\\59567\\AppData\\Local\\Temp\\ipykernel_27460\\2170489258.py:3: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  df['论文值'] = ''\n"
     ]
    }
   ],
   "source": [
    "def check_solved_eq(df):\n",
    "    df['验证'] = ['-' for i in range(len(df))]\n",
    "    df['论文值'] = ''\n",
    "    df.index = df['参数中文释义'].to_list()\n",
    "    data = zip(df['参数中文释义'].to_list(), df['解'].to_list())\n",
    "    dic = dict(data)\n",
    "    check_dic = {\n",
    "        '资本品名义产出':5375,\n",
    "        '资本品公司存款':5000,\n",
    "        '资本品公司贷款':1298,\n",
    "        '资本品公司税收':68.7,\n",
    "        '资本品公司存款利息':12.4,\n",
    "        '资本品公司贷款利息':9.7,\n",
    "        '资本品公司存货增加':3.7,\n",
    "        '资本品公司税前利润': 312.8 + 68.7,\n",
    "        '资本品公司税后利润':312.8,\n",
    "        '资本品公司股利': 312.8 - 31.3,\n",
    "        '资本品公司贷款增加': 9.7,\n",
    "        '消费品公司名义产出': 32971.4,\n",
    "        '居民部门名义消费': 32971.4,\n",
    "        '消费品公司存款': 25000,\n",
    "        '消费品公司存款利息': 62,\n",
    "        '消费品公司贷款' : 52194.4,\n",
    "        '消费品公司贷款利息': 388.5,\n",
    "        '消费品公司资本存量': 53863.6,\n",
    "        '消费品公司折旧': 4974,\n",
    "        '消费品公司利润': 2208.4 + 484.8,\n",
    "        '消费品公司交税': 484.8,\n",
    "        '消费品公司股利分配': 2208.4 - 220.8,\n",
    "        '银行股利': 179.1 - 71.7,\n",
    "        '银行税前利润': 179.1 + 39.3,\n",
    "        '消费品公司存货': 2997.4,\n",
    "        '居民存款': 80704.1,\n",
    "        '居民财富': 80704.1,\n",
    "        '居民存款利息': 200.3,\n",
    "        '居民收到股利': 2367.6,\n",
    "        '居民纳税数量': 7084.7,\n",
    "        '银行收到贷款利息': 398.2,\n",
    "        '银行纳税': 39.3,\n",
    "        '银行准备金': 28564.6,\n",
    "        '银行净值': 9626.4,\n",
    "        '就业群体收到工资额': 36800, \n",
    "        '银行收到债券利息': 95,\n",
    "        '政府支付债券利息': 165.9,\n",
    "        '政府发行债券': 66838.1,\n",
    "        '银行持有债券': 38273.5,\n",
    "        '中央银行持有债券': 28564.6,\n",
    "        '银行支付存款利息': 274.7,\n",
    "        '政府收税金额': 7677.4,\n",
    "        '中央银行利润': 70.9,\n",
    "        # '消费品公司贷款利息': 388.5,\n",
    "    }\n",
    "    dic['消费品公司存货'] = float(dic['消费品公司存货']) * float(dic['消费品单位生产成本(包括折旧)'])\n",
    "    for k, v in check_dic.items():\n",
    "        check_str = f\"{k}{dic[k]}, check_data{check_dic[k]}, 验证\"\n",
    "        if abs((float(dic[k]) - check_dic[k]) / check_dic[k]) < 0.005:\n",
    "            df.loc[k, '验证'] = \"通过验证\"\n",
    "            df.loc[k, '论文值'] = check_dic[k]\n",
    "        else:\n",
    "            df.loc[k, '验证'] = \"没通过验证\"\n",
    "            print(check_str, \"错误\")\n",
    "            df.loc[k, '论文值'] = check_dic[k]\n",
    "    return df\n",
    "df1 = df[df['设定情况']=='-']\n",
    "df1.index = list(range(len(df1)))\n",
    "df_check = check_solved_eq(df1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>参数中文释义</th>\n",
       "      <th>设定情况</th>\n",
       "      <th>解</th>\n",
       "      <th>para</th>\n",
       "      <th>验证</th>\n",
       "      <th>论文值</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>总就业人员数量</th>\n",
       "      <td>总就业人员数量</td>\n",
       "      <td>-</td>\n",
       "      <td>7360.000000</td>\n",
       "      <td>number_of_total_employment_workers</td>\n",
       "      <td>-</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>银行股利</th>\n",
       "      <td>银行股利</td>\n",
       "      <td>-</td>\n",
       "      <td>107.236245</td>\n",
       "      <td>dividends_of_banks</td>\n",
       "      <td>通过验证</td>\n",
       "      <td>107.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>居民总收入</th>\n",
       "      <td>居民总收入</td>\n",
       "      <td>-</td>\n",
       "      <td>33556.108225</td>\n",
       "      <td>net_income_of_households</td>\n",
       "      <td>-</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>居民纳税数量</th>\n",
       "      <td>居民纳税数量</td>\n",
       "      <td>-</td>\n",
       "      <td>7084.999367</td>\n",
       "      <td>tax_of_households</td>\n",
       "      <td>通过验证</td>\n",
       "      <td>7084.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>就业群体收到工资额</th>\n",
       "      <td>就业群体收到工资额</td>\n",
       "      <td>-</td>\n",
       "      <td>36800.000000</td>\n",
       "      <td>wages_of_households</td>\n",
       "      <td>通过验证</td>\n",
       "      <td>36800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>政府支付债券利息</th>\n",
       "      <td>政府支付债券利息</td>\n",
       "      <td>-</td>\n",
       "      <td>165.249046</td>\n",
       "      <td>government_pay_bonds_interests</td>\n",
       "      <td>通过验证</td>\n",
       "      <td>165.9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>政府收税金额</th>\n",
       "      <td>政府收税金额</td>\n",
       "      <td>-</td>\n",
       "      <td>7678.372654</td>\n",
       "      <td>government_collect_tax</td>\n",
       "      <td>通过验证</td>\n",
       "      <td>7677.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>中央银行利润</th>\n",
       "      <td>中央银行利润</td>\n",
       "      <td>-</td>\n",
       "      <td>71.129254</td>\n",
       "      <td>central_bank_profits</td>\n",
       "      <td>通过验证</td>\n",
       "      <td>70.9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>银行净值占资产比例</th>\n",
       "      <td>银行净值占资产比例</td>\n",
       "      <td>-</td>\n",
       "      <td>0.080000</td>\n",
       "      <td>ratio_of_bank_net_wealth_to_assets</td>\n",
       "      <td>-</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>消费品公司贷款利息</th>\n",
       "      <td>消费品公司贷款利息</td>\n",
       "      <td>-</td>\n",
       "      <td>388.225550</td>\n",
       "      <td>loan_interests_of_consumption_firm</td>\n",
       "      <td>通过验证</td>\n",
       "      <td>388.5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>62 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "              参数中文释义 设定情况             解                                para  \\\n",
       "总就业人员数量      总就业人员数量    -   7360.000000  number_of_total_employment_workers   \n",
       "银行股利            银行股利    -    107.236245                  dividends_of_banks   \n",
       "居民总收入          居民总收入    -  33556.108225            net_income_of_households   \n",
       "居民纳税数量        居民纳税数量    -   7084.999367                   tax_of_households   \n",
       "就业群体收到工资额  就业群体收到工资额    -  36800.000000                 wages_of_households   \n",
       "...              ...  ...           ...                                 ...   \n",
       "政府支付债券利息    政府支付债券利息    -    165.249046      government_pay_bonds_interests   \n",
       "政府收税金额        政府收税金额    -   7678.372654              government_collect_tax   \n",
       "中央银行利润        中央银行利润    -     71.129254                central_bank_profits   \n",
       "银行净值占资产比例  银行净值占资产比例    -      0.080000  ratio_of_bank_net_wealth_to_assets   \n",
       "消费品公司贷款利息  消费品公司贷款利息    -    388.225550  loan_interests_of_consumption_firm   \n",
       "\n",
       "             验证     论文值  \n",
       "总就业人员数量       -          \n",
       "银行股利       通过验证   107.4  \n",
       "居民总收入         -          \n",
       "居民纳税数量     通过验证  7084.7  \n",
       "就业群体收到工资额  通过验证   36800  \n",
       "...         ...     ...  \n",
       "政府支付债券利息   通过验证   165.9  \n",
       "政府收税金额     通过验证  7677.4  \n",
       "中央银行利润     通过验证    70.9  \n",
       "银行净值占资产比例     -          \n",
       "消费品公司贷款利息  通过验证   388.5  \n",
       "\n",
       "[62 rows x 6 columns]"
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_check"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_check.to_excel('output/check6.xlsx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {},
   "outputs": [],
   "source": [
    "sv = solved_variables"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 检验消费品公司资金流量表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 22.3131739151478$"
      ],
      "text/plain": [
       "22.3131739151478"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 消费品公司存货增加（资金流出）\n",
    "sv['consumption_firm_inventory'] * sv['unit_cost_of_consumption_firm'] * sv['oo2_steady_state_rate']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 186.104218362283$"
      ],
      "text/plain": [
       "186.104218362283"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 消费品公司存款增加（资金流出）\n",
    "sv['deposits_consumption_firm_holds'] * sv['oo2_steady_state_rate']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 221.1615498143$"
      ],
      "text/plain": [
       "221.161549814300"
      ]
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 消费品公司存留收益（资金流入）\n",
    "sv['profits_of_consumption_firm'] - sv['tax_of_consumption_firms'] - sv['dividends_of_consumption_firms']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 4974.03029268187$"
      ],
      "text/plain": [
       "4974.03029268187"
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 消费品公司折旧（资金流入）\n",
    "sv['amortization_of_consumption_firms']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 388.225549781259$"
      ],
      "text/plain": [
       "388.225549781259"
      ]
     },
     "execution_count": 112,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 消费品公司贷款增加(资金流入)\n",
    "sv['loans_of_consumption_firms'] * sv['oo2_steady_state_rate']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 检验居民部门的资金流量表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 598.819483619939$"
      ],
      "text/plain": [
       "598.819483619939"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 存款（净财富）的增加\n",
    "sv['deposits_of_households'] * sv['oo2_steady_state_rate']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 36800.0$"
      ],
      "text/plain": [
       "36800.0000000000"
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 居民收到的工资\n",
    "sv['households_wage'] * sv['number_of_total_employment_workers']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 199.60649453998$"
      ],
      "text/plain": [
       "199.606494539980"
      ]
     },
     "execution_count": 115,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 居民收到的存款利息\n",
    "sv['deposits_of_households'] * sv['interest_rate_of_deposits'] * sv['oo1_steady_state_rate']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 2379.21235562886$"
      ],
      "text/plain": [
       "2379.21235562886"
      ]
     },
     "execution_count": 116,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 居民收到的股利\n",
    "(sv['dividends_of_banks'] + sv['dividends_of_capital_firms'] + sv['dividends_of_consumption_firms'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 107.236245085391$"
      ],
      "text/plain": [
       "107.236245085391"
      ]
     },
     "execution_count": 117,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 居民收到的银行股利\n",
    "sv['dividends_of_banks']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 1990.45394832871$"
      ],
      "text/plain": [
       "1990.45394832871"
      ]
     },
     "execution_count": 118,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 居民收到消费公司股利\n",
    "sv['dividends_of_consumption_firms']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 281.522162214762$"
      ],
      "text/plain": [
       "281.522162214762"
      ]
     },
     "execution_count": 119,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 居民收到资本公司股利\n",
    "sv['dividends_of_capital_firms']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 7084.9993665489$"
      ],
      "text/plain": [
       "7084.99936654890"
      ]
     },
     "execution_count": 120,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 居民交税\n",
    "(\n",
    "sv['households_wage'] * sv['number_of_total_employment_workers'] + \n",
    "sv['deposits_of_households'] * sv['interest_rate_of_deposits'] * sv['oo1_steady_state_rate'] +\n",
    "(sv['dividends_of_banks'] + sv['dividends_of_capital_firms'] + sv['dividends_of_consumption_firms']) * sv['oo1_steady_state_rate']\n",
    ")* sv['tax_rate']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 检验银行利润表\n",
    "## 公式31-33\n",
    "$\\pi_b = i^l \\frac{L_c + L_k}{1 + g_{ss}} + i^b \\frac{B_b}{1 + g_{ss}} - i^d\\frac{D_h + D_c + D_k}{1 + g_{ss}}$ 银行利润计算 <br>\n",
    "$T_b = \\pi_b\\tau_b$ 银行税收<br>\n",
    "$Div_b = \\rho_b[\\pi_b(1-\\tau_b)]$ 银行股利"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "消费品公司 388.225549781259\n",
      "资本品公司 9.66268779361758\n"
     ]
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 397.888237574877$"
      ],
      "text/plain": [
       "397.888237574877"
      ]
     },
     "execution_count": 121,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 银行收到贷款利息\n",
    "print('消费品公司', sv['loan_interests_of_consumption_firm'])\n",
    "print('资本品公司', sv['loan_interests_of_capital_firm'])\n",
    "sv['bank_receive_loans_interests']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "消费品公司 62.0347394540943\n",
      "资本品公司 12.4069478908189\n",
      "居民 199.606494539980\n"
     ]
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 274.048181884893$"
      ],
      "text/plain": [
       "274.048181884893"
      ]
     },
     "execution_count": 122,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print('消费品公司', sv['deposits_interests_of_consumption_firm'])\n",
    "print('资本品公司', sv['deposits_interests_of_capital_firm'])\n",
    "print('居民', sv['deposits_interests_of_households'])\n",
    "sv['bank_pay_deposits_interests']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "银行持有债券系数 403.000000000000\n"
     ]
    }
   ],
   "source": [
    "print('银行持有债券系数',(1 + sv['steady_state_rate']) / sv['interest_rate_of_bonds'])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.19"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
